package com.loveprogrammer.battle;

import com.loveprogrammer.battle.action.Action;
import com.loveprogrammer.battle.action.BaseAttack;

import java.util.LinkedList;

/**
 * @ClassName Battle
 * @Description
 * @Author admin
 * @Date 2024/2/20 17:03
 * @Version 1.0
 */
public class BattleOneToOne {

    // 正式战斗是以秒为单位，但是我们不可能真的按照秒来进行战斗，会大大的影响服务器的性能，所以我们应该要做的就是缩短打架的过程把结果返回给客户端即可，并把实际打斗时间告诉给客户端，然后客户端再进行时延的播放即可

    // 设计一个队列，在
    private LinkedList<Action> actions = new LinkedList<>();

    private int addAction(Action action){
        actions.offer(action);
        return actions.size();
    }

    public void fight(Hero A,Hero B) throws InterruptedException {
        // 根据速度放入队列
        if(A.getSpeed() > B.getSpeed()){
            addAction(new BaseAttack(A,B));
            addAction(new BaseAttack(B,A));
        }else{
            addAction(new BaseAttack(B,A));
            addAction(new BaseAttack(A,B));
        }

        while(A.getHp() > 0 && B.getHp() > 0) {
            Action pop = actions.pop();
            pop.run();
            // 再放进去
            if (pop.checkContinue()) {
                addAction(pop);
            }
        }

        if(A.getHp() > 0) {
            System.out.println(A.getName() + "获胜:" + A.getHp());
        }else{
            System.out.println(B.getName() + "获胜:" + B.getHp());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BattleOneToOne battleOneToOne = new BattleOneToOne();

        Hero A = new Hero("刘备");
        Hero B = new Hero("关羽");
        B.setSpeed(20);
        B.setAttack(20);
        battleOneToOne.fight(A,B);
    }

}
